home *** CD-ROM | disk | FTP | other *** search
/ STraTOS 1997 April & May / STraTOS 1 - 1997 April & May.iso / CD01 / INTERNET / SITES / RAND / DVIEW000.LZH / ORIGINAL.SRC / VIEW_IO.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-23  |  6.5 KB  |  233 lines

  1. /********************************************************************
  2.  FILENAME: VIEW_IO.CPP
  3.  AUTHOR  : JAKE HILL
  4.  DATE    : 12/1/94
  5.  
  6.  Copyright (c) 1994 by Jake Hill:
  7.  If you use any part of this code in your own project, please credit
  8.  me in your documentation and source code.  Thanks.
  9. ********************************************************************/
  10.  
  11. #include "VIEW.HPP"
  12. #include "TRIG.HPP"
  13.  
  14. #include <io.h>
  15. #include <dos.h>                // int86
  16. #include <fcntl.h>
  17. #include <conio.h>              // kbhit, getch
  18. #include <stdio.h>              // printf
  19. #include <string.h>
  20. #include <process.h>            // exit
  21.  
  22. // Some more yucky global variables.
  23. short NumThings;
  24. short NumSegs;
  25. short NumSides;
  26. short NumLines;
  27. short NumNodes;
  28. short NumSectors;
  29. short NumSSectors;
  30. short NumVertexes;
  31. short BlockmapSize;
  32.  
  33. // Sets the graphics mode.
  34. void setmode(unsigned short n)
  35. {
  36.    union REGS inregs, outregs;
  37.  
  38.    inregs.x.ax = n;
  39.    int86(0x10, &inregs, &outregs);
  40. };
  41.  
  42. // This function opens the wadfile, determines if it is a PWAD
  43. // or an IWAD, then reads in the resources for THINGS, LINES, SIDES,
  44. // VERTEXES, SEGS, SSECTORS, NODES, and SECTORS.
  45. void View::OpenWad(char *WadName, long Level)
  46. {
  47.    int handle;
  48.    short EntriesRead = 0;
  49.    unsigned long EntryOffset = 0;
  50.  
  51.    WAD_Header HEADER;
  52.    Directory_Entry DIRECTORY;
  53.  
  54.    handle = open( WadName ,O_BINARY|O_RDONLY);
  55.    if (handle == (-1))
  56.    {
  57.       printf("ERROR:Could not open file %s.\n",WadName);
  58.       exit(-1);
  59.    }
  60.  
  61.    read(handle, &HEADER, sizeof(WAD_Header));
  62.  
  63.    if ( strnicmp(HEADER.signature, "IWAD", 4) == 0 )
  64.    {
  65.       printf("Reading IWAD file.\n");
  66. // We need to skip past the PLAYPAL, and DEMO stuff if in an IWAD.
  67.       HEADER.foffset += 6L * sizeof(Directory_Entry);
  68.    }
  69.    else if ( strnicmp(HEADER.signature, "PWAD", 4) == 0 )
  70.    {
  71.       printf("Reading PWAD file.\n");
  72.    }
  73.    else
  74.    {
  75.       printf("This is not a valid WAD file!\n");
  76.       exit(-1);
  77.    }
  78.  
  79.    printf("This file contains %d directory entries.\n\n", HEADER.num_entries);
  80.    EntryOffset += HEADER.foffset + (Level*11L*sizeof(Directory_Entry));
  81.  
  82.    while ( EntriesRead < 11 )
  83.    {
  84.       lseek(handle, EntryOffset, SEEK_SET);
  85.       read(handle, &DIRECTORY, sizeof(Directory_Entry));
  86.  
  87.       if ( strnicmp( DIRECTORY.name, "THINGS", 6 ) == 0 )
  88.          LoadThings( &DIRECTORY, handle );
  89.       else if ( strnicmp( DIRECTORY.name, "LINEDEFS", 8 ) == 0 )
  90.          LoadLines( &DIRECTORY, handle );
  91.       else if ( strnicmp( DIRECTORY.name, "SIDEDEFS", 8 ) == 0 )
  92.          LoadSides( &DIRECTORY, handle );
  93.       else if ( strnicmp( DIRECTORY.name, "VERTEXES", 8 ) == 0 )
  94.          LoadVertexes( &DIRECTORY, handle );
  95.       else if ( strnicmp( DIRECTORY.name, "SEGS", 4 ) == 0 )
  96.          LoadSegs( &DIRECTORY, handle );
  97.       else if ( strnicmp( DIRECTORY.name, "SSECTORS", 8 ) == 0 )
  98.          LoadSSectors( &DIRECTORY, handle );
  99.       else if ( strnicmp( DIRECTORY.name, "NODES", 5 ) == 0 )
  100.          LoadNodes( &DIRECTORY, handle );
  101.       else if ( strnicmp( DIRECTORY.name, "SECTORS", 7 ) == 0 )
  102.          LoadSectors( &DIRECTORY, handle );
  103.       else
  104.          printf("Skipping %8s.\n",DIRECTORY.name);
  105.  
  106.       EntriesRead++;
  107.       EntryOffset += sizeof( Directory_Entry );
  108.    }
  109.    printf("Allocated memory for arrays...\n");
  110.  
  111.    close(handle);
  112.    InitTrig();
  113.  
  114.    printf("Press Any key to continue...\n");
  115.    while ( !kbhit() );
  116.    getch();
  117.  
  118.    setmode(19);
  119. };
  120.  
  121. // All of the following procedures read in the indicated
  122. // resource from a WAD file pointed to by handle.
  123.  
  124. void View::LoadThings( Directory_Entry *Dir, int handle )
  125. {
  126.    float angle;
  127.    NumThings = (short) (Dir->size / sizeof(thing));
  128.    thing *Thing_Array = new thing [ NumThings ];
  129.  
  130.    lseek(handle, Dir->foffset, SEEK_SET);
  131.    read(handle,  Thing_Array, (unsigned int) Dir->size);
  132.  
  133.    for (short i=0; i<NumThings; i++)
  134.       if ( Thing_Array[i].thing_type == 1 )
  135.       {
  136.          angle = (float) (182.0444444444444 * (float) Thing_Array[i].angle);
  137.          SetView( Thing_Array[i].x, Thing_Array[i].y, 40, (unsigned short) angle);
  138.          break;
  139.       }
  140.  
  141.    printf("THINGS   : %d\n", NumThings);
  142.    delete [] Thing_Array;
  143. }
  144.  
  145. void View::LoadSegs( Directory_Entry *Dir, int handle )
  146. {
  147.    NumSegs = (short) (Dir->size / sizeof(seg));
  148.    Seg_Array = new seg [ NumSegs ];
  149.  
  150.    lseek(handle, Dir->foffset, SEEK_SET);
  151.    read(handle,  Seg_Array, (unsigned int) Dir->size);
  152.  
  153.    printf("SEGS     : %d\n", NumSegs);
  154. }
  155.  
  156. void View::LoadSides( Directory_Entry *Dir, int handle )
  157. {
  158.    NumSides = (short) Dir->size/sizeof(side);
  159.    Side_Array = new side [ NumSides ];
  160.  
  161.    lseek(handle, Dir->foffset, SEEK_SET);
  162.    read(handle,  Side_Array, (unsigned int) Dir->size);
  163.  
  164.    printf("SIDEDEFS : %d\n", NumSides);
  165. }
  166.  
  167. void View::LoadLines( Directory_Entry *Dir, int handle )
  168. {
  169.    NumLines   = (short) (Dir->size / sizeof(line));
  170.    Line_Array = new line [ NumLines ];
  171.  
  172.    lseek(handle, Dir->foffset, SEEK_SET);
  173.    read(handle,  Line_Array, (unsigned int)Dir->size);
  174.  
  175.    printf("LINEDEFS : %d\n", NumLines);
  176. }
  177.  
  178. void View::LoadNodes( Directory_Entry *Dir, int handle )
  179. {
  180.    NumNodes = (short) Dir->size/sizeof(node);
  181.    Node_Array = new node [ NumNodes ];
  182.    PNode_Array = new node * [ NumNodes ];
  183.  
  184.    MaxNode  = NumNodes - 1;
  185.  
  186.    lseek(handle, Dir->foffset, SEEK_SET);
  187.    read(handle,  Node_Array, (unsigned int) Dir->size);
  188.  
  189.    for (int i=0; i<NumNodes; i++)
  190.       PNode_Array[i] = &Node_Array[i];
  191.  
  192.    printf("NODES    : %d\n", NumNodes);
  193. }
  194.  
  195. void View::LoadSectors( Directory_Entry *Dir, int handle )
  196. {
  197.    NumSectors = (short) Dir->size/sizeof(sector);
  198.    Sector_Array = new sector [ NumSectors ];
  199.  
  200.    lseek(handle, Dir->foffset, SEEK_SET);
  201.    read(handle,  Sector_Array, (unsigned int) Dir->size);
  202.  
  203.    printf("SECTORS  : %d\n", NumSectors);
  204. }
  205.  
  206. void View::LoadVertexes( Directory_Entry *Dir, int handle )
  207. {
  208.    NumVertexes = (short) Dir->size/sizeof(vertex);
  209.    Vertex_Array = new vertex [ NumVertexes ];
  210.  
  211.    lseek(handle, Dir->foffset, SEEK_SET);
  212.    read(handle,  Vertex_Array, (unsigned int) Dir->size);
  213.  
  214.    printf("VERTEXES : %d\n", NumVertexes);
  215. }
  216.  
  217. void View::LoadSSectors( Directory_Entry *Dir, int handle )
  218. {
  219.    NumSSectors = (short) Dir->size/sizeof(ssector);
  220.    SSector_Array = new ssector [ NumSSectors ];
  221.  
  222.    lseek(handle, Dir->foffset, SEEK_SET);
  223.    read(handle,  SSector_Array, (unsigned int) Dir->size);
  224.  
  225.    printf("SSECTORS : %d\n", NumSSectors);
  226. }
  227.  
  228. // Returns the screen mode to normal text.
  229. void View::Close(void)
  230. {
  231.    setmode(3);
  232. };
  233.